import 'package:base_plugin/base_plugin.dart';
import 'package:finance_app/generated/a.dart';
import 'package:finance_app/src/common/app_colors.dart';
import 'package:finance_app/src/widgets/pop/drow_down_menu.dart';
import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';

class NormalTitleBorderPop extends StatefulWidget {
  final String select;
  final String title;
  final List<String> group;
  final String hint;
  final ValueChanged<String> onSelect;
  final bool isTop;
  final double horizontalPadding;

  const NormalTitleBorderPop(
      {Key key,
      this.title,
      this.horizontalPadding,
      this.select,
      this.group,
      this.hint,
      this.onSelect,
      this.isTop = false})
      : super(key: key);

  @override
  _NormalTitleBorderPopState createState() => _NormalTitleBorderPopState();
}

class _NormalTitleBorderPopState extends State<NormalTitleBorderPop> {
  DropDownMenuController controller = DropDownMenuController();
  String _select;

  @override
  void initState() {
    super.initState();
    _select = widget.select;
    controller.addListener(() {
      setState(() {});
    });
  }

  @override
  void didUpdateWidget(covariant NormalTitleBorderPop oldWidget) {
    if (widget.select != _select) {
      setState(() {
        _select = widget.select;
      });
    }
    super.didUpdateWidget(oldWidget);
  }

  @override
  void dispose() {
    super.dispose();
    controller.dispose();
  }

  Widget _buildIndustryItem(String e) {
    return PressedOpacityButton(
      onPressed: () {
        widget.onSelect?.call(e);
        controller.close();
        setState(() {
          _select = e;
        });
      },
      color: e == _select ? aceWhiteGreyColor : null,
      padding: EdgeInsets.symmetric(horizontal: getDp(15), vertical: getDp(19)),
      child: Text(
        e,
        style: TextStyle(
          color: aceBlackColor,
          fontSize: getSp(16),
          fontWeight: FontWeight.w700,
          height: 1,
        ),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: EdgeInsets.symmetric(
        horizontal: widget.horizontalPadding ?? getDp(35),
      ),
      child: DropDownMenu(
        isTop: widget.isTop,
        isStackChild: true,
        isDisabled: widget.onSelect == null,
        controller: controller,
        dropDownMenu: Container(
          clipBehavior: Clip.antiAliasWithSaveLayer,
          decoration: BoxDecoration(
            color: Colors.white,
            border: Border.all(
              color: aceGrayColor,
              width: getDp(0.5),
            ),
            borderRadius: BorderRadius.circular(getDp(15)),
          ),
          child: Column(
            children: [
              if (widget.isTop == true) ...[
                Container(
                  constraints: BoxConstraints(
                    maxHeight: getDp(280),
                  ),
                  child: ListView.builder(
                    shrinkWrap: true,
                    padding: EdgeInsets.zero,
                    itemCount: widget.group.length,
                    itemBuilder: (BuildContext context, int index) =>
                        _buildIndustryItem(widget.group[index]),
                  ),
                ),
                Divider(
                  height: 1,
                ),
                PressedOpacityButton(
                    onPressed: () {
                      controller.close();
                    },
                    child: buildSelectInput(isPop: true)),
              ] else ...[
                PressedOpacityButton(
                    onPressed: () {
                      controller.close();
                    },
                    child: buildSelectInput(isPop: true)),
                Divider(
                  height: 1,
                ),
                Container(
                  constraints: BoxConstraints(
                    maxHeight: getDp(280),
                  ),
                  child: ListView.builder(
                    shrinkWrap: true,
                    padding: EdgeInsets.zero,
                    itemCount: widget.group.length,
                    itemBuilder: (BuildContext context, int index) =>
                        _buildIndustryItem(widget.group[index]),
                  ),
                )
              ]
            ],
          ),
        ),
        child: buildSelectInput(isPop: controller.isOpen),
      ),
    );
  }

  Widget buildSelectInput({bool isPop = false}) {
    return Container(
        padding: EdgeInsets.symmetric(
          horizontal: getDp(12),
        ),
        height: getDp(54),
        decoration: BoxDecoration(
          color: Colors.white,
          border: isPop
              ? null
              : Border.all(
                  color: aceGrayColor,
                  width: getDp(0.5),
                ),
          borderRadius: isPop
              ? widget.isTop == true
                  ? BorderRadius.only(
                      bottomLeft: Radius.circular(getDp(15)),
                      bottomRight: Radius.circular(getDp(15)),
                    )
                  : BorderRadius.only(
                      topLeft: Radius.circular(getDp(15)),
                      topRight: Radius.circular(getDp(15)),
                    )
              : BorderRadius.circular(getDp(15)),
        ),
        child: Row(
          children: [
            Expanded(
                child: _select == null
                    ? Text(
                        widget.hint,
                        style: TextStyle(
                          color: widget.onSelect == null
                              ? aceBlackColor
                              : aceBlueColor,
                          fontSize: getSp(14),
                          fontWeight: FontWeight.w700,
                          height: 16 / 14,
                        ),
                      )
                    : Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        mainAxisSize: MainAxisSize.min,
                        children: [
                          Text(
                            widget.title,
                            style: TextStyle(
                              color: aceDarkGrayColor,
                              fontSize: getSp(12),
                              fontWeight: FontWeight.w400,
                              height: 16 / 12,
                            ),
                          ),
                          SizedBox(
                            height: getDp(3),
                          ),
                          Text(
                            _select,
                            style: TextStyle(
                              color: widget.onSelect == null
                                  ? aceBlackColor
                                  : aceBlueColor,
                              fontSize: getSp(16),
                              fontWeight: FontWeight.w700,
                              height: 1,
                            ),
                          )
                        ],
                      )),
            if (widget.onSelect != null)
              SvgPicture.asset(
                A.assetsImagesStyleArrowBlack,
                width: getDp(4.6),
                height: getDp(6),
                fit: BoxFit.contain,
                color: aceBlueColor,
              ),
          ],
        ));
  }
}
